<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改品牌</title>
</head>
<body>
<h3>修改品牌</h3>
<form action="" method="post">
  品牌名称：<input id="brandName" name="brandName"><br>
  企业名称：<input id="companyName" name="companyName"><br>
  排序：<input id="ordered" name="ordered"><br>
  描述信息：<textarea rows="5" cols="20" id="description" name="description"></textarea><br>
  状态：
  <input type="radio" name="status" value="0">禁用
  <input type="radio" name="status" value="1" >启用<br>

  <input type="button" id="btn"  value="修改" onclick="sub()">
</form>
</body>
</html>
<script src="js/jquery-3.7.1.js"></script>
    <script>
        $(function () {
            scById()
        })
        var urlParams = new URLSearchParams(window.location.search);
        var id = urlParams.get('id');
        //根据ID查询
        function scById() {

            $.ajax({
                url:"selectByIdServlet?id="+id,
                type: "GET",
                success:function (data) {
                    console.log(data)
                    var brand = JSON.parse(data);
                    console.log(brand);
                    $("#brandName").val(brand.brandName);
                    $("#companyName").val(brand.companyName);
                    $("#ordered").val(brand.ordered);
                    $("#description").val(brand.description);
                    if (brand.status == 1) {
                        $("input[name='status'][value='1']").prop("checked", true);
                    } else {
                        $("input[name='status'][value='0']").prop("checked", true);
                    }
                }
            })
        }

        //修改提交按钮
        function sub() {
            var brandName = $("#brandName").val();
            var companyName = $("#companyName").val();
            var ordered = $("#ordered").val();
            var description = $("#description").val();
            var status = $("input[name='status']:checked").val()
            $.ajax({
                url:"updateBrandServlet",
                type:"POST",
                data:{
                    id:id,
                    brandName: brandName,
                    companyName: companyName,
                    ordered: ordered,
                    description: description,
                    status: status
                },
                success:function (data) {
                    window.location.href='brand.jsp';
                }
            })
        }
    </script>